<?php

declare(strict_types=1);

namespace App\Helper;

use Hyperf\Context\Context;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\DbConnection\Db;

class Calendar
{
    /**
     * 获取一周日历
     * [getWeek description]
     * @param  [type] $first_day [description]
     * @return [type]            [description]
     */
    public static function getWeek($first_date="")
    {
        if(empty($first_date)){
            $first_date = date('Y-m-d', strtotime("this week"));
        }
        $week = [];
        for ($i=0; $i < 7; $i++) { 
            $week[] = date('Y-m-d', strtotime($first_date." +".$i." day"));
        }
        return $week;
    }
    /**
     * 获取一周日历-只返开始和结束时间
     * [getWeek description]
     * @param  [type] $first_day [description]
     * @return [type]            [description]
     */
    public static function getWeekJustBeginAndEnd($first_date="")
    {
        $data = self::getWeek($first_date);
        $return_data = [
            'first_date'=>$data[0],
            'last_date'=>$data[count($data)-1],
        ];
        return $return_data;
    }
    /**
     * 获取一月日历-包含前后周
     * [getComMonth description]
     * @param  string $month [description]
     * @return [type]        [description]
     */
    public static function getComMonth($month="")
    {
        if(empty($month)){
            $month = date("Y-m");
        }
        //拼接前一周
        $first_day = $month."-01";
        $first_day_in_week = date('w', strtotime($first_day));
        $before_week = [];
        if($first_day_in_week==0){
            $count_first_idx = 6;
        }else{
            $count_first_idx = $first_day_in_week-1;
        }
        for ($i=$count_first_idx; $i>0; $i--) { 
            $before_week[] = date('Y-m-d', strtotime($first_day." -".$i." day"));
        }
        //拼接后一周
        $next_month_first_day = date('Y-m-01', strtotime($month." +1 month"));
        $last_day = date('Y-m-d', strtotime($next_month_first_day." -1 day"));
        $last_day_in_week = date('w', strtotime($last_day));
        $after_week = [];
        if($last_day_in_week!=0){
            $count_last_idx = 1;
            for ($i=$last_day_in_week; $i<=6; $i++) { 
                $after_week[] = date('Y-m-d', strtotime($last_day." +".$count_last_idx." day"));
                $count_last_idx++;
            }
        }
        $month_data = self::getMonthDays($month);
        $data = array_merge($before_week,$month_data,$after_week);
        return $data;
    }
    /**
     * 获取一月日历-包含前后月
     * [getMonth description]
     * @param  string $month [description]
     * @return [type]        [description]
     */
    public static function getMonth($month="")
    {
        if(empty($month)){
            $month = date("Y-m");
        }
        $last_month = date('Y-m', strtotime($month." -1 month"));
        $next_month = date('Y-m', strtotime($month." +1 month"));
        $last_month_data = self::getMonthDays($last_month);
        $month_data = self::getMonthDays($month);
        $next_month_data = self::getMonthDays($next_month);
        $data = array_merge($last_month_data,$month_data,$next_month_data);
        return $data;
    }
    /**
     * 获取一个月的所有日期
     * [getMonthDays description]
     * @param  string $month [description]
     * @return [type]        [description]
     */
    public static function getMonthDays($month)
    {
        $data = [];
        $days = date('t', strtotime($month));
        for ($i=1; $i<=$days; $i++) { 
            if(strlen((string)$i)==1){
                $data[] = $month."-0".$i;
            }else{
                $data[] = $month."-".$i;
            }
        }
        return $data;
    }
    /**
     * 根据开始和结束时间组装日历
     * [getCalByBeginAndEnd description]
     * @param  [type] $first_date [description]
     * @param  [type] $last_date  [description]
     * @return [type]             [description]
     */
    public static function getCalByBeginAndEnd($first_date,$last_date)
    {
        if(empty($first_date) || empty($last_date)){
            return NULL;
        }
        $data = [];
        $flag = true;
        $add_day = 0;
        $max_day = 60;
        while ($flag) {
            $res_day = date('Y-m-d', strtotime($first_date." +".$add_day." day"));
            $data[] = $res_day;
            $add_day++;
            if($res_day==$last_date){
                $flag = false;
            }
            if($add_day>$max_day){
                $flag = false;
            }
        }
        return $data;
    }
}
